package main

import (
	"fmt"
	"github.com/gin-gonic/gin"
	_ "github.com/mkevac/debugcharts" // 可选，添加后可以查看几个实时图表数据
	"go.uber.org/zap"
	"net/http"
	_ "net/http/pprof" // 必须，引入 pprof 模块
	"wj-go-web/routers"
)

//TIP <p>To run your code, right-click the code and select <b>Run</b>.</p> <p>Alternatively, click
// the <icon src="AllIcons.Actions.Execute"/> icon in the gutter and select the <b>Run</b> menu item from here.</p>

var logger *zap.Logger

func main() {
	// 1.创建路由
	r := gin.Default()

	routers.RegisterUser(r)

	// 2.绑定路由规则，执行的函数
	// gin.Context，封装了request和response
	r.GET("/", func(c *gin.Context) {
		c.String(http.StatusOK, "hello World!")
	})

	go func() {
		//内存分析网址
		//https://blog.csdn.net/weixin_45565886/article/details/132090495

		// terminal: $ go tool pprof -http=:8081 http://localhost:6060/debug/pprof/heap
		// web:
		// 1、http://localhost:8081/ui
		// 2、http://localhost:6060/debug/charts
		// 3、·
		fmt.Println(http.ListenAndServe("0.0.0.0:6060", nil))
	}()

	//初始化日志组件
	InitLogger()

	// 3.监听端口，默认在8080
	// Run("里面不指定端口号默认为8080")
	r.Run(":8000")
}

func InitLogger() {
	logger, _ = zap.NewProduction()
}

func simpleHttpGet(url string) {
	resp, err := http.Get(url)
	if err != nil {
		logger.Error(
			"Error fetching url..",
			zap.String("url", url),
			zap.Error(err))
	} else {
		logger.Info("Success..",
			zap.String("statusCode", resp.Status),
			zap.String("url", url))
		resp.Body.Close()
	}
}
